import numpy as np


def sigmoid(x):
    return 1/(1+np.exp(-x))

def sigmoid_grad(x):
    return (1.0-sigmoid(x))*sigmoid(x)

def softmax(x):
    if x.ndim == 2:
        x -= x.max(axis=1,keepdims=True)
        exp_x = np.exp(x)
        return exp_x/np.sum(exp_x,axis=1,keepdims=True)
    x -= np.max(x)
    return np.exp(x)/np.sum(np.exp(x))

def cross_entropy_error(y,t):
    if y.ndim == 1:
        t = t.reshape(1,t.size)
        y = y.reshape(1,y.size)

    if t.size == y.size:
        t = t.argmax(axis=1)

    batch_size = y.shape[0]
    return -np.sum(np.log(y[np.arange(batch_size),t]+1e-7))/batch_size

if __name__ == '__main__':
    t = np.array([[0,0,1,0,0,0,0,0,0,0],[0,0,1,0,0,0,0,0,0,0]])
    y = np.array([[0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0],[0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0]])

    print(cross_entropy_error(y,t))